﻿using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace AJWPFAdmin.Core.Components.AJTable.ViewModels
{
    /// <summary>
    /// 分页配置模型
    /// </summary>
    public class AJTablePagination : BindableBase
    {
        private int _total;
        private int _current;
        private int _pageSize;
        private int[] _pageSizeOptions;

        private HorizontalAlignment _hAlign;

        private ObservableCollection<AJTablePageItem> _pageItems;

        private bool _canPrevPage;
        private bool _canNextPage;

        private int _totalPages;

        /// <summary>
        /// 数据总数
        /// </summary>
        public int Total
        {
            get { return _total; }
            set
            {
                if (SetProperty(ref _total, value))
                {
                    CalculatePageItems();
                }
            }
        }
        /// <summary>
        /// 当前页,默认1
        /// </summary>
        public int Current
        {
            get { return _current; }
            set
            {
                if (SetProperty(ref _current, value))
                {
                    CalculatePageItems();
                }
            }
        }
        /// <summary>
        /// 每页条数, 默认10
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                if (SetProperty(ref _pageSize, value))
                {
                    CalculatePageItems();
                }
            }
        }
        /// <summary>
        /// 指定每页可以显示多少条,选项下拉框
        /// </summary>
        public int[] PageSizeOptions
        {
            get { return _pageSizeOptions; }
            set
            {
                if (SetProperty(ref _pageSizeOptions, value))
                {
                    var firstItem = _pageSizeOptions.FirstOrDefault();
                    if (firstItem != 0)
                    {
                        PageSize = firstItem;
                    }
                }
            }
        }

        /// <summary>
        /// 位置对齐,默认居右
        /// </summary>
        public HorizontalAlignment Alignment { get { return _hAlign; } set { SetProperty(ref _hAlign, value); } }

        /// <summary>
        /// 是否能上一页
        /// </summary>
        public bool CanPrevPage { get { return _canPrevPage; } set { SetProperty(ref _canPrevPage, value); } }

        /// <summary>
        /// 是否能上下一页
        /// </summary>
        public bool CanNextPage { get { return _canNextPage; } set { SetProperty(ref _canNextPage, value); } }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages { get { return _totalPages; } }

        /// <summary>
        /// 分页器页码按钮条目数组 如: 1,2,3
        /// </summary>
        public ObservableCollection<AJTablePageItem> PageItems
        {
            get { return _pageItems; }
            set { SetProperty(ref _pageItems, value); }
        }

        public AJTablePagination()
        {
            Current = 1;
            PageSize = 10;
            PageSizeOptions = new int[] { 10, 50, 100, 500, 1000 };
            Alignment = HorizontalAlignment.Center;
            CalculatePageItems();
        }

        private void CalculatePageItems()
        {
            var items = new List<AJTablePageItem>();

            if (Total == 0)
            {
                if (Current != 1)
                {
                    Current = 1;
                    return;
                }
                CanPrevPage = false;
                CanNextPage = false;
                PageItems = new ObservableCollection<AJTablePageItem>(items);
                return;
            }

            _totalPages = Convert.ToInt32(Math.Floor(((double)Total - 1) / PageSize) + 1);

            if (Current > _totalPages)
            {
                Current = 1;
            }

            CanPrevPage = Current > 1;
            CanNextPage = Current < _totalPages;

            var pageBufferSize = 2;

            if (_totalPages <= 5 + pageBufferSize * 2)
            {
                for (var i = 1; i <= _totalPages; i++)
                {
                    items.Add(new AJTablePageItem
                    {
                        PageNumber = i.ToString(),
                        Checked = Current == i
                    });
                }
            }
            else
            {
                var jumpPrev = new AJTablePageItem
                {
                    PageNumber = "···",
                    ToolTip = "向前5页"
                };
                var jumpNext = new AJTablePageItem
                {
                    PageNumber = "···",
                    ToolTip = "向后5页"
                };

                var lastPager = new AJTablePageItem
                {
                    PageNumber = _totalPages.ToString()
                };
                var firstPager = new AJTablePageItem
                {
                    PageNumber = "1"
                };

                var left = new int[] { 1, Current - pageBufferSize }.Max();
                var right = new int[] { Current + pageBufferSize, _totalPages }.Min();

                if (Current - 1 <= pageBufferSize)
                {
                    right = 1 + pageBufferSize * 2;
                }

                if (_totalPages - Current <= pageBufferSize)
                {
                    left = _totalPages - pageBufferSize * 2;
                }

                for (int i = left; i <= right; i++)
                {
                    items.Add(new AJTablePageItem
                    {
                        PageNumber = i.ToString(),
                        Checked = i == Current
                    });
                }

                if (Current - 1 >= pageBufferSize * 2 && Current != 1 + 2)
                {
                    items[0].Checked = false;
                    items[0].PageNumber = left.ToString();
                    items.Insert(0, jumpPrev);
                }

                if (_totalPages - Current >= pageBufferSize * 2 && Current != _totalPages - 2)
                {
                    items[items.Count - 1].Checked = false;
                    items[items.Count - 1].PageNumber = right.ToString();
                    items.Add(jumpNext);
                }

                if (left != 1)
                {
                    items.Insert(0, firstPager);
                }
                if (right != _totalPages)
                {
                    items.Add(lastPager);
                }
            }
            PageItems = new ObservableCollection<AJTablePageItem>(items);
        }

    }
}
